package cn.fl.dk.web.controller;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.dk.facade.condition.DkQuestneLogQC;
import cn.fl.dk.facade.constant.DkQuestneLogConstant;
import cn.fl.dk.web.service.IDkQuestneLogService;
import cn.fl.dk.web.service.ITemplateService;
import cn.fl.dk.web.vo.DkQuestneLogVO;
import cn.fl.dk.web.vo.TemplateVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.service.ISysUsrService;
import cn.fl.system.vo.SysScoreRecVO;
import cn.fl.system.vo.SysUsrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 问卷记录表(DK_QUESTNE_LOG)  questionnaire 控制层
 */
@Controller
@RequestMapping("/dk/dkquestnelog")
public class DkQuestneLogController extends BaseController {

	//private static final Logger logger = LoggerFactory.getLogger(DkQuestneLogController.class);

	/**
	 * DkQuestneLog 服务层BEAN
	 */
	@Autowired
	private IDkQuestneLogService dkQuestneLogService;
    @Autowired
    private ITemplateService templateService ;
    @Autowired
    private ISysUsrService usrService ;
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.newLogger("加载列表页面").setLogOperType(LogOperEnum.REQ).setKeys("toListPage",this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName()).info();

		ModelAndView view = new ModelAndView("dk/dkQuestneLog/dkQuestneLogList");
		// 查询页面显示使用的字典
		// 问卷状态
		List<KeyValueVO> qtnStsCdList = this.dkQuestneLogService.getDictionaryList(DkQuestneLogConstant.GROUP_CD_QTN_STS_CD);
		// 任务分配状态 0未分配 1 已分配
		// 任务分配状态暂时就不配字典了
		// 问卷结果
		List<KeyValueVO> qtnResultList = this.dkQuestneLogService.getDictionaryList(DkQuestneLogConstant.GROUP_CD_QTN_RST_CD);
		// 是否相符
		List<KeyValueVO> isMatchList = this.dkQuestneLogService.getDictionaryList(DkQuestneLogConstant.GROUP_CD_IS_MATCH);
		view.addObject("qtnStsCdList",qtnStsCdList);
		view.addObject("qtnResultList",qtnResultList);
		view.addObject("isMatchList",isMatchList);
		return view;
	}


	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response,DkQuestneLogQC qc) {
		BaseLogUtils.newLogger("加载列表页面").setLogOperType(LogOperEnum.REQ).setKeys("initList",this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName()).info();
		// 分页参数
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}
		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}
		page.setCondition(qc);
		// 如果当前人是  垫款试卷管理员 可以看全部，否则只能看自己的数据
		Long currentRoleId = CurrentThreadContext.getCurrentRoleId();
		// 垫款试卷管理员roleId
		Long MANAGER = 1804250005L;
		if(!currentRoleId .equals(MANAGER) ){
			// 非管理员只能查询自己的问卷信息。
			qc.setCstMgrId(CurrentThreadContext.getCurrentUserId());
		}

		Pager pager = this.dkQuestneLogService.searchListPage(page);
		List<DkQuestneLogVO> list = (List<DkQuestneLogVO>) pager.getResultList();
		ListPageVO<DkQuestneLogVO> listPage = new ListPageVO<DkQuestneLogVO>(list, pager.getTotalCount());
		return listPage;
	}


	/**
	 * 查看试卷结果
	 * @param ruleFlag  额，其实是ruleFlag
	 * @param model
	 * @return
	 */
	@RequestMapping("view.do")
	public String view(String ruleFlag,Model model){
		// 根据RuleId查询答卷日志
		DkQuestneLogVO dkQuestneLogVO = this.dkQuestneLogService.selectByRuleFlag(ruleFlag);
		// 从评分记录中 查询templateId
		SysScoreRecVO sysScoreRecVO = this.templateService.selectRecByPkId(dkQuestneLogVO.getId());
		// 获取templateVO 用来显示
		TemplateVO template = this.templateService.getTemplate(String.valueOf(sysScoreRecVO.getTmplId()), String.valueOf(dkQuestneLogVO.getId()));
		model.addAttribute("template",template);
		model.addAttribute("id",dkQuestneLogVO.getId());

		return "/dk/dkQuestneLog/view";
	}

    /**
     * 已出险问卷作答
     * @param id  questneLogId
     * @return
     */
    @RequestMapping("/examA.do")
    public String examA(String id,Model model ){
	    // 问题模板id
        Long validTemplateId = this.templateService.getValidTemplateId("1");
        String templateId = "180620158000261";
        if(validTemplateId != null){
            templateId = String.valueOf(validTemplateId);
        }
        TemplateVO templateVO = templateService.getTemplate(templateId,id);
		model.addAttribute("template",templateVO);
		model.addAttribute("id",id);
        return "/dk/dkQuestneLog/add";
    }

    /**
     *  未出险问卷作答
     * @param id questneLogId
     * @return
     */
    @RequestMapping("/examB.do")
    public String examB(String id,Model model){
    	// 模板id
	    String templateId="180620158000262";
        Long validTemplateId = this.templateService.getValidTemplateId("0");
        if(validTemplateId!=null){
            templateId = String.valueOf(validTemplateId);
        }
        // 头部信息
        TemplateVO templateVO = templateService.getTemplate(templateId,id);
        model.addAttribute("template",templateVO);
        model.addAttribute("id",id);
        return "/dk/dkQuestneLog/add";
    }

	/**
	 *
	 * @param id
	 * @param status 保存状态
	 * @param result 分数计算结果
	 * @param isMatch 分数计算是否相符
	 */
    @RequestMapping("/update.do")
	@ResponseBody
    public void update(String id,String status,String result,String isMatch){

		DkQuestneLogVO vo = new DkQuestneLogVO();
		vo.setId(Long.valueOf(id));
		vo.setQtnStsCd(status);
		if(status.equals("2")){ //暂存不更新结果
			vo.setQtnRstCd(result);
		}else{
			vo.setQtnRstCd(DkQuestneLogConstant.QTN_RST_CD_1);
		}
		vo.setIsMatch(isMatch);
		this.dkQuestneLogService.modify(vo);
	}

	/**
	 * 分配任务页面
	 * @return
	 */
	@RequestMapping("/toAssignPage.do")
	public String toAssignPage(){
		return "/dk/dkQuestneLog/assign";
	}

    /**
     *  分配任务人列表查询 ajax
     * @param usrNm 查询条件:用户名称
     * @param page
     * @param pagesize
     * @return
     */
	@RequestMapping("assignPeopleList.do")
    @ResponseBody
	public ListPageVO<SysUsrVO> assignPeopleList(String usrNm,Integer page ,Integer pagesize){
		Pager pager = new Pager();
		SysUsrQueryCondition qc = new SysUsrQueryCondition();
		pager.setEveryPage(pagesize);
		pager.setCurrentPage(page);
		if(StringUtils.isNotBlank(usrNm)){
			qc.setRlNm(usrNm);
		}
//		qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
		pager.setCondition(qc);
		Pager res = usrService.searchListPage(pager);
		List<SysUsrVO> resultList = (List<SysUsrVO>) res.getResultList();
        ListPageVO<SysUsrVO> listPage = new ListPageVO<SysUsrVO>(resultList, res.getTotalCount());
        return listPage;
	}

    /**
     * 分配任务
     * @param id 问卷日志id
     * @param usrId 用户id
     */
	@RequestMapping("assignTask.do")
	@ResponseBody
    public void assignTask(String id ,String usrId){
	    this.dkQuestneLogService.assignTask(id,usrId);
    }

    /**
     * 查看问卷页面
     * @param ruleFlag ruleFlag
     * @param model
     * @return
     */
    @RequestMapping("toUpdatePage.do")
    public String toUpdatePage(String ruleFlag ,Model model){
        // 根据RuleId查询答卷日志
        DkQuestneLogVO dkQuestneLogVO = this.dkQuestneLogService.selectByRuleFlag(ruleFlag);
        // 从评分记录中 查询templateId
        SysScoreRecVO sysScoreRecVO = this.templateService.selectRecByPkId(dkQuestneLogVO.getId());
        // 获取templateVO 用来显示
        TemplateVO template = this.templateService.getTemplate(String.valueOf(sysScoreRecVO.getTmplId()), String.valueOf(dkQuestneLogVO.getId()));
        model.addAttribute("template",template);
        model.addAttribute("id",dkQuestneLogVO.getId());
        return "/dk/dkQuestneLog/update";
    }

}

